package com.zhiyou100.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou100.model.Result;
import com.zhiyou100.model.User;
import com.zhiyou100.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService us;	
	//注册
	@RequestMapping("/regist.do")
	@ResponseBody
	public Result register(User user, HttpSession session) {
		Result result = new Result();
		//查询是否存在该用户
		int i=us.selectUserByEmail(user.getEmail());
		if(i>0) {
			//System.out.println("用户存在");
			result.setSuccess(false);
			result.setMessage("用户已存在！");
			return result;
		}else {
			//插入用户和密码
			int j=us.insertUser(user);
			if(j>0) {
				//注册成功显示用户名
				System.out.println("注册成功");
				result.setSuccess(true);
				User user1=us.selectUserById(user.getId());
				session.setAttribute("_front_user", user1);
				return result;
								
			}else {
				//System.out.println("注册失败");
				result.setSuccess(false);
				result.setMessage("注册失败！");
				return result;
			}					
		}
		
		
	}
	
	//登录
	@RequestMapping("/login.do")
	@ResponseBody
	public Result login(User user,HttpSession session) {
		Result result = new Result();
		//查询用户名和密码是否正确
		int i=us.checkUser(user);
		if(i>0) {
			//System.out.println("登录成功");
			result.setSuccess(true);
			session.setAttribute("_front_user", user);
			return result;
		}else {
			//System.out.println("登录失败");
			result.setSuccess(false);
			result.setMessage("登录失败！");
			return result;
		}
	}
	
	//个人资料
	@RequestMapping("/data")
	public String data(HttpSession session) {
		User u = (User)session.getAttribute("_front_user");
		User user1=us.selectUserInfoByEmail(u.getEmail());	
		session.setAttribute("user", user1);		
		return "forward:/front/user/index.jsp";
	}
	
	//修改资料
	@RequestMapping("/profile")
	public String profile(User user ,HttpSession session) {
		user.setUpdate_time(new Date());
		User u = (User)session.getAttribute("_front_user");
		user.setEmail(u.getEmail());//从session中拿到用户名
		int i=us.updateUserInfo(user);
		System.out.println(i);
		if(i>0) {
			session.setAttribute("user", user);	
			return "forward:/front/user/index.jsp";
		}else {
			System.out.println("更新失败");
			return "redirect:/front/user/profile.jsp";
		}
		
	}
	
	// 更换头像
	@RequestMapping("/avatar")
	public String avatar(MultipartFile image_file,HttpServletRequest request,HttpSession session,User user) {
		String name2=null;
		try {
			InputStream is=image_file.getInputStream();
			String path=request.getServletContext().getRealPath("/images");
			System.out.println(path);
			
			name2=image_file.getOriginalFilename();			
			File file =new File(path,name2);			
			OutputStream os=new FileOutputStream(file);			
			IOUtils.copy(is, os);		
			os.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//更新数据库图片路径
		User u = (User)session.getAttribute("_front_user");
		user.setEmail(u.getEmail());
		user.setHead_url("images/"+name2);
		int i=us.updateUserHeadurl(user);
		
		if(i>0) {
			System.out.println("上传头像成功");
			session.setAttribute("_front_user", user);
			return "forward:/front/user/index.jsp";
		}else {
			System.out.println("上传失败");
			return "redirect:/front/user/avatar.jsp";
		}		
		
	}
	
	public String getFileName(String filename) {
		
		String extension=FilenameUtils.getExtension(filename);
		return UUID.randomUUID().toString()+"."+extension;
	}
	
	//密码安全
	@RequestMapping("/password")
	public String password(@RequestParam String oldPassword,@RequestParam String newPassword,@RequestParam String newPasswordAgain,
			HttpSession session,User user) {
		
		
		return null;
	}
	
	
}
